<?php

namespace App\Common\Eloquent;

trait Query
{


	/**
	 * SQL·构造字段
	 * @param $key string 字段名
	 * @param $pref string 字段前缀
	 * */
	static function sql_tb_field ($key, $pref='') {
		return $pref = $pref ? $pref.'.'.$key : $key;
	}


    /**
     * 分批insert
     * @param array $dat
     * @param int $size
     */
    static function insertByChunk (array $dat, $size=666)
    {
        \DB::beginTransaction();
        $dat = array_chunk($dat, $size);
        foreach ($dat as &$group) {
            static::insert($group);
        }
        \DB::commit();
    }


    /**
     * 获取Model的数据表
     * @return string
     */
    static function table ()
    {
        return (new self())->getTable();
    }


    /**
     * 分批replace
     * @param array $dat
     * @param int $size
     */
    static function replaceByChunk (array $dat, $size=666)
    {
        \DB::beginTransaction();
        $dat = array_chunk($dat, $size);
        foreach ($dat as &$group) {
            static::replace($group);
        }
        \DB::commit();
    }

}
